package com.xl.testHadfAPI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Test;

import java.net.URI;


/**
 * @ClassName testFileUpload
 * @Description TODO
 * @Author hxl
 * @Date 2021/3/22 18:07
 * Version 1.0
 **/
public class testHdfsApi {

  /**
   * @Description:  测试上传文件到HDFS
   * @Author: hxl
   * @Date: 2021/3/22 18:28
   **/
  @Test
  public void testUploadFile() throws Exception {
    //得配置本地的hostname
    String uri = "hdfs://hadoop100:9000";
    Configuration conf = new Configuration();
    //操作hdfs的用户
    String user = "root";
    // 1.获取fs
    FileSystem fs = FileSystem.get(new URI(uri),conf,user);
    // 2.操作API
    fs.copyFromLocalFile(false,new Path("D:/testHdfs.txt"),new Path("/"));
    //3. 关闭资源
    fs.close();
  }

  /**
   * @Description: 测试删除HDFS上的文件
   * @Author: hxl 
   * @Date: 2021/3/22 18:28
   **/
  @Test
  public void testDelFile() throws Exception {
    Configuration conf = new Configuration();
    // 1.获取fs
    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"),conf,"root");
    // 2.操作API
    fs.delete(new Path("/testHdfs.txt"),true);
    //3. 关闭资源
    fs.close();
  }

  /**
   * @Description: 测试下载HDFS上的文件
   * @Author: hxl
   * @Date: 2021/3/22 18:28
   **/
  @Test
  public void testDowloadFile() throws Exception {
    Configuration conf = new Configuration();
    // 1.获取fs
    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"),conf,"root");
    // 2.操作API
    // 2 执行下载操作
    // boolean delSrc 指是否将原文件删除
    // Path src 指要下载的文件路径
    // Path dst 指将文件下载到的路径
    // boolean useRawLocalFileSystem 是否开启文件校验
    fs.copyToLocalFile(false, new Path("/user/hxl/log.txt"), new Path("d:/log.txt"), true);

    //3. 关闭资源
    fs.close();
  }

  /**
   * @Description: 测试HDFS文件名修改
   * @Author: hxl
   * @Date: 2021/3/22 18:28
   **/
  @Test
  public void testUpdateFileName() throws Exception {
    Configuration conf = new Configuration();
    // 1.获取fs
    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"),conf,"root");
    // 2.操作API
    fs.rename(new Path("/user/hxl/log.txt"),new Path("/user/hxl/newlog.txt"));
    //3. 关闭资源
    fs.close();
  }

  /**
   * @Description: 测试HDFS文件详情查看
   * @Author: hxl
   * @Date: 2021/3/22 18:28
   **/
  @Test
  public void testlistFile() throws Exception {
    Configuration conf = new Configuration();
    // 1.获取fs
    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"),conf,"root");
    // 2.操作API
    // 2 获取文件详情
    RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
    while(listFiles.hasNext()){
      LocatedFileStatus status = listFiles.next();
      // 输出详情
      // 文件名称
      System.out.println(status.getPath().getName());
      // 长度
      System.out.println(status.getLen());
      // 权限
      System.out.println(status.getPermission());
      // 分组
      System.out.println(status.getGroup());
      // 获取存储的块信息
      BlockLocation[] blockLocations = status.getBlockLocations();
      for (BlockLocation blockLocation : blockLocations) {
        // 获取块存储的主机节点
        String[] hosts = blockLocation.getHosts();

        for (String host : hosts) {
          System.out.println(host);
        }
      }

      System.out.println("-----------的分割线----------");
    }

    //3. 关闭资源
    fs.close();
  }

  /**
   * @Description: 测试HDFS文件和文件夹判断
   * @Author: hxl
   * @Date: 2021/3/22 18:28
   **/
  @Test
  public void testListStatus() throws Exception {
    Configuration conf = new Configuration();
    // 1.获取fs
    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"),conf,"root");
    // 2.操作API
    // 2 判断是文件还是文件夹
    FileStatus[] listStatus = fs.listStatus(new Path("/"));

    for (FileStatus fileStatus : listStatus) {
      // 如果是文件
      if (fileStatus.isFile()) {
        System.out.println("f:"+fileStatus.getPath().getName());
      }else {
        System.out.println("d:"+fileStatus.getPath().getName());
      }
    }
    //3. 关闭资源
    fs.close();
  }

}
